package v1

import (
	"fmt"
	"gin-project/model"
	"gin-project/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InsertSongList(ctx *gin.Context) {
	title := ctx.PostForm("title")
	introduction := ctx.PostForm("introduction")
	style := ctx.PostForm("style")

	fmt.Println(title)
	fmt.Println(introduction)
	fmt.Println(style)

	var songList model.SongList
	songList.Title = title
	songList.Introduction = introduction
	songList.Style = style
	code := model.InsertSongList(&songList)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func SelectSongListAll(ctx *gin.Context) {
	songList, code := model.SelectSongListAll()
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songList,
	})
}

func UpdateSongList(ctx *gin.Context) {
	songListID := ctx.PostForm("id")
	title := ctx.PostForm("title")
	introduction := ctx.PostForm("introduction")
	style := ctx.PostForm("style")

	code := model.UpdateSongList(map[string]interface{}{
		"title": title,
		"introduction": introduction,
		"style": style,
	}, songListID)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func UpdateSongListPic(ctx *gin.Context) {
	file, header, _ := ctx.Request.FormFile("file")
	id := ctx.PostForm("id")

	url, _ := model.UpdateSongListPic(file, header)
	code := model.UpdateSongList(map[string]interface{}{
		"pic": url,
		}, id)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func DeleteSongList(ctx *gin.Context) {
	songListID := ctx.Query("id")
	fmt.Println(songListID)

	code := model.DeleteSongList(songListID)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func InsertSongToSongList(ctx *gin.Context)  {
	songListID := ctx.PostForm("songListId")
	songID := ctx.PostForm("songId")

	code := model.InsertSongToSongList(songListID, songID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

func SelectSongListOfSongs(ctx *gin.Context) {
	songListID := ctx.Query("songListId")

	songList, code := model.SelectSongListByID(songListID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songList,
	})
	return
}

func DeleteSongListOfSong(ctx *gin.Context) {
	songID := ctx.Query("songId")
	songListID := ctx.Query("songListId")
	fmt.Println("获取到的歌曲ID是", songID)
	fmt.Println("获取到的歌单ID是", songListID)

	code := model.DeleteSongListOfSong(songListID, songID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
	})
	return
}

// 根据标题模糊查询歌单
func SelectLikeTitle(ctx *gin.Context) {
	title := ctx.Query("title")
	songLists, code := model.SelectLikeTitle(title)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songLists,
	})
}

// 根据歌单ID查询歌单下的歌曲
func SelectSongsBySongListID(ctx *gin.Context) {
	songListID := ctx.Query("songListId")
	fmt.Println(songListID)

	songs, code := model.SelectSongsBySongListID(songListID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": errmsg.GetErrMsg(code),
		"data": songs,
	})
	return
}